//
//  RBMeSquareButton.m
//  Budejie
//
//  Created by 华杨科技 on 2017/10/13.
//  Copyright © 2017年 饶波. All rights reserved.
//

#import "RBMeSquareButton.h"
#import "RBMeSquare.h"
#import <UIImageView+WebCache.h>
#import <UIButton+WebCache.h>

@implementation RBMeSquareButton

// 字体相关属性只需要设置一次，放在 initWithFrame方法中最合适
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont systemFontOfSize:13.0];
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self setBackgroundImage:[UIImage imageNamed:@"mainCellBackground"] forState:UIControlStateNormal];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
 
    // 图片位置
    self.imageView.rb_y = self.rb_width * 0.2 ;// 比例系数，
    self.imageView.rb_height = self.rb_width * 0.5;
    self.imageView.rb_width = self.imageView.rb_height;
    self.imageView.rb_center_x = self.rb_height * 0.5;
    
    // label位置
    self.titleLabel.rb_x = 0;
    self.titleLabel.rb_y = self.imageView.rb_bottom;
    self.titleLabel.rb_width = self.rb_width;
//    self.titleLabel.rb_height = self.rb_height * 0.2;
    self.titleLabel.rb_height = self.rb_height - self.titleLabel.rb_y;
}

- (void)setSquare:(RBMeSquare *)square 
{
    _square = square;
    //        [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:square.icon] options:SDWebImageDownloaderLowPriority progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
    //            [button setImage:image forState:UIControlStateNormal];
    //        }];
    [self sd_setImageWithURL:[NSURL URLWithString:square.icon] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"setup-head-default"]];
   
    [self setTitle:square.name forState:UIControlStateNormal];
}
@end
